﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace GLUpload2
{
    public class APIUtils
    {
        public static IntPtr GetXLMainWindowHandle(IntPtr DesktopHandle)
        {
            return FindMainWindowInProcess(DesktopHandle, String.Empty, "XLMAIN");
        }

        internal static IntPtr FindMainWindowInProcess(IntPtr HWNDParent, string WindowName, string WindowClass)
        {
            IntPtr FoundWindow = IntPtr.Zero;

            string FindClass = String.Empty;
            string FindName = String.Empty;
            uint WindowProcessID;

            IntPtr tempWindow = GetWindow(HWNDParent, GW_CHILD);
            while ((int)tempWindow > 0)
            {
                FindName = GetWindowText(tempWindow);
                FindClass = GetClassName(tempWindow);

                bool CompareClass = ((FindClass.IndexOf(WindowClass) >= 0) || (WindowClass == String.Empty));
                bool CompareCaption = ((FindName.IndexOf(WindowName) >= 0) || (WindowName == String.Empty));

                if (CompareClass && CompareCaption)
                {
                    GetWindowThreadProcessId(tempWindow, out WindowProcessID);
                    if (GetCurrentProcessId() == WindowProcessID)
                    {
                        FoundWindow = tempWindow;
                        if (IsWindowVisible(FoundWindow))
                        {
                            break;
                        }
                    }
                }
                tempWindow = GetWindow(tempWindow, GW_HWNDNEXT);
            }
            return FoundWindow;
        }

        [DllImport("user32.dll")]
        public static extern IntPtr SetFocus(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDesktopWindow();

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetWindow(IntPtr hwnd, int uCmd);

        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hwnd,
            [In][Out]StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern int GetClassName(IntPtr hwnd,
            [In][Out]StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern bool IsWindowVisible(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        [DllImport("kernel32.dll")]
        public static extern uint GetCurrentProcessId();

        public static string GetWindowText(IntPtr handle)
        {
            System.Text.StringBuilder lpBuffer = new System.Text.StringBuilder(255);
            if (GetWindowText(handle, lpBuffer, 255) > 0)
            {
                return lpBuffer.ToString();
            }
            else
                return "";
        }

        public static string GetClassName(IntPtr handle)
        {
            System.Text.StringBuilder className = new System.Text.StringBuilder(255);
            if (GetClassName(handle, className, 255) > 0)
            {
                return className.ToString();
            }
            else
                return "";
        }

        public const int GW_CHILD = 5;
        public const int GW_HWNDNEXT = 2;

    } 
}
